package com.evry.marshallingtest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.evry.marshallingtest.newmodel.Customer;
import com.evry.marshallingtest.oldmodel.Database;
import com.evry.marshallingtest.oxm.marshaller.Jaxb2Marshaller;
import com.evry.marshallingtest.oxm.marshaller.Marshaller;
import com.evry.marshallingtest.support.DataFinder;
import com.evry.marshallingtest.support.DataSupport;
import com.evry.marshallingtest.support.TransformDataLogic;

/**
 * Hello world!
 *
 */
public class App 
{
	private static ApplicationContext appContext =
            new ClassPathXmlApplicationContext("App.xml");
//	private static Model oldModel = (Model) appContext.getBean("oldModel");
//	private static com.evry.marshallingtest.newmodel.Model newModel = (com.evry.marshallingtest.newmodel.Model) appContext.getBean("newModel");
	
    public static void main( String[] args )
    {
    	
    	
    	//Import necessary beans
    	final DataSupport dataSupport = (DataSupport) appContext.getBean("dataSupport");
    	final Marshaller application = (Jaxb2Marshaller) appContext.getBean("marshaller");
    	final DataFinder dataFinder = (DataFinder) appContext.getBean("dataFinder");
    	final TransformDataLogic transformer = (TransformDataLogic) appContext.getBean("transformDataLogic");
    	// initiate mock data in old model
    	Database oldDatabase = dataSupport.populateDataBase();
    	
    	System.out.println("Order info retrieved from old database, order with index 0");
    	System.out.println("First name of customer: " + oldDatabase.getOrders().getOrder().get(0).getCustomer().getFirstName());
    	
    	// prepare information and set down to the new database model, to be sent to marshaller to process.
    	com.evry.marshallingtest.newmodel.Database newDatabase = transformer.createNewDataBaseFromOld(oldDatabase);
    	Customer customer = dataFinder.findCustomerWithPersonalCodeNumber(newDatabase, newDatabase.getOrders().getOrder().get(0).getPersonalCodeNumber());
		System.out.println("Order info retrieved from new database, order with index 0");
		System.out.println("First name of customer: " + customer.getFirstName());
    	String xml = application.marshal(newDatabase);
    	System.out.println(xml);
    	
    }
    
    

}
